#include <myhead.h>
#include <sqlite3.h>
#include "dao.h"

extern sqlite3* db;

int tbl_friend_getState(char* username, char* friendname,int * state){
	char sql[]="select state from tbl_friend where myname=? and friendname=?;";
	sqlite3_stmt *pStmt;
	if(sqlite3_prepare_v2(db,sql,sizeof(sql), &pStmt, NULL)!=SQLITE_OK){
		fprintf(stderr, "tbl_friend_getState error:%s\n", sqlite3_errmsg(db));
		return -1;
	}

	sqlite3_bind_text(pStmt, 1, username, strlen(username), NULL);
	sqlite3_bind_text(pStmt, 2, friendname, strlen(friendname), NULL);

	if(sqlite3_step(pStmt)==SQLITE_ROW){
		*state=sqlite3_column_int(pStmt, 0);
		sqlite3_finalize(pStmt);
		return 1;
	}
	sqlite3_finalize(pStmt);
	return 0;
} 

int tbl_friend_insert(char* username, char* friendname, int state){
	char sql[]="insert into tbl_friend (myname,friendname,state)values (?,?,?);";

	sqlite3_stmt *pStmt;
	if(sqlite3_prepare_v2(db,sql,sizeof(sql), &pStmt, NULL)!=SQLITE_OK){
		fprintf(stderr, "tbl_friend_insert error:%s\n", sqlite3_errmsg(db));
		return -1;
	}
	
	sqlite3_bind_text(pStmt,1, username, strlen(username), NULL);
	sqlite3_bind_text(pStmt,2, friendname, strlen(friendname), NULL);
	sqlite3_bind_int(pStmt,3, state);

	int retStep=sqlite3_step(pStmt);
	printf("tbl_friend_insert. retStep=[%d]\n", retStep);

	sqlite3_finalize(pStmt);
	return 0;
}

int tbl_friend_updateState(char* username, char* friendname, int state){
	char sql[]="update tbl_friend set state=? where myname=? and friendname=?;";

	sqlite3_stmt *pStmt;
	if(sqlite3_prepare_v2(db,sql,sizeof(sql), &pStmt, NULL)!=SQLITE_OK){
		fprintf(stderr, "tbl_friend_updateState error:%s\n", sqlite3_errmsg(db));
		return -1;
	}
	
	sqlite3_bind_int(pStmt, 1, state);
	sqlite3_bind_text(pStmt, 2, username, strlen(username), NULL);
	sqlite3_bind_text(pStmt, 3, friendname, strlen(friendname), NULL);

	int retStep=sqlite3_step(pStmt);
	printf("tbl_friend_updateState. retStep=[%d]\n", retStep);

	sqlite3_finalize(pStmt);
}

int tbl_friend_getList(char* username, int state, db_user_t* dataArr, int* arrLen){
	char sql[]="select myname,friendname,state from tbl_friend where myname=? and state=? limit ?";

	sqlite3_stmt *pStmt;
	if(sqlite3_prepare_v2(db,sql,sizeof(sql), &pStmt, NULL)!=SQLITE_OK){
		fprintf(stderr, "tbl_friend_getFriendList error:%s\n", sqlite3_errmsg(db));
		return -1;
	}

	sqlite3_bind_text(pStmt, 1, username, strlen(username), NULL);
	sqlite3_bind_int(pStmt, 2, state);
	sqlite3_bind_int(pStmt, 3, *arrLen);

	//int stepRet=sqlite3_step(pStmt);
	//printf("stepRet=[%d],SQLITE_ROW=[%d],SQLITE_DONE=[%d]\n", stepRet, SQLITE_ROW, SQLITE_DONE);
	int cnt=0;
	while(sqlite3_step(pStmt)==SQLITE_ROW){
		strcpy(dataArr[cnt].username, sqlite3_column_text(pStmt, 0));
		//借助passwd传输好友名称
		strcpy(dataArr[cnt].friendname, sqlite3_column_text(pStmt, 1));
		cnt++;
	}
	*arrLen=cnt;
	sqlite3_finalize(pStmt);
	return 0;
}

int tbl_friend_getNum(char* username, int state){
	printf("??????? username=[%s],state=[%d]\n", username, state);
	char sql[]="select count(*) as cnt from tbl_friend where friendname=? and state=?;";

	sqlite3_stmt *pStmt;
	if(sqlite3_prepare_v2(db,sql,sizeof(sql), &pStmt, NULL)!=SQLITE_OK){
		fprintf(stderr, "tbl_friend_getFriendNum error:%s\n", sqlite3_errmsg(db));
		return -1;
	}

	sqlite3_bind_text(pStmt, 1, username, strlen(username), NULL);
	sqlite3_bind_int(pStmt, 2, state);

	//int stepRet=sqlite3_step(pStmt);
	//printf("stepRet=[%d],SQLITE_ROW=[%d],SQLITE_DONE=[%d]\n", stepRet, SQLITE_ROW, SQLITE_DONE);
	int cnt=-1;
	if(sqlite3_step(pStmt)==SQLITE_ROW){
		cnt=sqlite3_column_int(pStmt, 0);
	}else{
		printf("%s\n", sqlite3_errmsg(db));
	}
	sqlite3_finalize(pStmt);
	return cnt;
}
